{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sisl\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example of the $k$-point sampling for TBtrans."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = sisl.Geometry([0]*3, sisl.Atom(1, R=1.), lattice=[1, 1, 10])\n",
    "chain.set_nsc([3, 3, 1])\n",
    "\n",
    "# Transport along y-direction\n",
    "chain = chain.tile(20, 0)\n",
    "He = sisl.Hamiltonian(chain)\n",
    "He.construct(([0.1, 1.1], [0, -1]))\n",
    "Hd = He.tile(20, 1)\n",
    "He.write('ELEC.nc')\n",
    "Hd.write('DEVICE.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('RUN.fdf', 'w') as f:\n",
    "    f.write(\"\"\"\n",
    "TBT.k [ 3 1 1 ]\n",
    "\n",
    "TBT.DOS.A\n",
    "TBT.Current.Orb\n",
    "\n",
    "TBT.HS DEVICE.nc\n",
    "%block TBT.Elec.Left\n",
    "  HS ELEC.nc\n",
    "  semi-inf-direction -a2\n",
    "  electrode-position 1\n",
    "%endblock\n",
    "%block TBT.Elec.Right\n",
    "  HS ELEC.nc\n",
    "  semi-inf-direction +a2\n",
    "  electrode-position end -1\n",
    "%endblock\n",
    "    \"\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run these two executables:\n",
    "\n",
    "    tbtrans -D TRS RUN.fdf\n",
    "    tbtrans -D NO_TRS -fdf TBT.Symmetry.TimeReversal:f RUN.fdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trs = sisl.get_sile('TRS/siesta.TBT.nc')\n",
    "no_trs = sisl.get_sile('NO_TRS/siesta.TBT.nc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_bond(tbt, E):\n",
    "    xy = tbt.geometry.xyz[:, :2]\n",
    "    vector = tbt.vector_transmission(E, 0)[:, :2]\n",
    "    atom = tbt.atom_transmission(E, 0)\n",
    "    # Normalize atomic current\n",
    "    atom += 1\n",
    "    atom *= 10 / atom.max()\n",
    "    plt.scatter(xy[:, 0], xy[:, 1], atom);\n",
    "    plt.quiver(xy[:, 0], xy[:, 1], vector[:, 0], vector[:, 1]);\n",
    "    plt.gca().get_xaxis().set_visible(False)\n",
    "    plt.gca().get_yaxis().set_visible(False)\n",
    "    plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_bond(trs, 1.)\n",
    "plt.savefig('fig/trs.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_bond(no_trs, 1.)\n",
    "plt.savefig('fig/no_trs.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
